package com.scommunity.admin.controller;

import com.github.pagehelper.PageInfo;
import com.scommunity.common.ResponseResult;
import com.scommunity.domain.Prop;
import com.scommunity.service.IPropService;
import com.scommunity.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/prop")
public class PropController {
    @Autowired
    private IPropService iPropService;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @PostMapping("/add")
    public Object add(@RequestBody Prop prop) {
        //String propNum = "scprop" + prop.getPropId();
        long propId = snowflakeIdWorker.nextId();
        prop.setPropId(propId);
       // prop.setPropNum(propNum);
        Prop nprop = iPropService.add(prop);
        return ResponseResult.success(nprop);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody Prop prop) {
        iPropService.delete(prop);
    }

    @PostMapping("/update")
    public void update(@RequestBody Prop prop) {
        iPropService.updateN(prop);
    }

    @GetMapping("/all")
    public Object all(Integer page) {
        if (page == null) {
            page = 1;
        }
        Prop prop = new Prop();
        PageInfo<Prop> pageInfo = iPropService.all(page, 8);
        if (pageInfo == null) {
            return ResponseResult.success();
        } else {
            return ResponseResult.success(pageInfo);
        }
    }

    @PostMapping("/findByCondition")
    public Object findByCondition(@RequestBody Prop prop) {
        PageInfo<Prop> pageInfo = iPropService.getPageByCondition(prop, 1, 8);
        if (pageInfo == null) {
            return ResponseResult.success();
        } else {
            return ResponseResult.success(pageInfo);
        }
    }
}
